from .view import ParserGroup, ParserGroupView, ParserRuleView, ParserRule, ParserLibView, ParserLib, ParserMapView, ParserMap
from . import schema
from typing import List
from fastapi import APIRouter, Depends, Security
from package.connector.sql_db import db
from package.fastapi import permission


router_transform = APIRouter(prefix='/transform', tags=['数据转换'])
router_meta = {'module': '数据采集', 'sub_module': '数据转换'}


@router_transform.get('/group', response_model=schema.PageParserGroups, openapi_extra=router_meta, name='group_list_get',
                 summary='数据转换组列表')
def group_list_get(q: str = None,
                   page: int = 1,
                   page_size: int = 10,
                   order_by: str = 'created_at desc',
                   session=Depends(db.get_session),
                   user=Security(permission.security, scopes=['group:read'])):
    return ParserGroupView.get_page_items(session, q=q, page=page, page_size=page_size, order_by=order_by)


@router_transform.post('/group', response_model=schema.ParserGroupOut, openapi_extra=router_meta, name='group_post', summary='数据转换组创建')
def group_post(body: schema.CreateParserGroup,
               session=Depends(db.get_session),
               user=Security(permission.security, scopes=['group:write'])):
    return ParserGroupView.create_item(session, body=body.dict())


@router_transform.get('/group/{id}', response_model=schema.ParserGroupOut, openapi_extra=router_meta, name='group_get',
                 summary='数据转换组详情')
def group_get(id: str,
              session=Depends(db.get_session),
              user=Security(permission.security, scopes=['group:read'])):
    return ParserGroupView.get_item(session, filter_=[ParserGroup.id == id])


@router_transform.patch('/group/{id}', response_model=schema.ParserGroupOut, openapi_extra=router_meta, name='group_patch',
                   summary='数据转换组修改')
def group_patch(id: str,
                body: schema.EditParserGroup,
                session=Depends(db.get_session),
                user=Security(permission.security, scopes=['group:write'])):
    return ParserGroupView.update_group(session, filter_=[ParserGroup.id == id], update=body.dict(exclude_unset=True))


@router_transform.delete('/group', openapi_extra=router_meta, name='group_delete', summary='数据转换组删除')
def group_delete(body: schema.BatchId,
                session=Depends(db.get_session),
                user=Security(permission.security, scopes=['group:write'])):
    return {'count': ParserGroupView.delete_groups(session, filter_=[ParserGroup.id.in_(body.ids)])}


@router_transform.get('/group/{id}/rule', response_model=List[schema.ParserRuleOut], openapi_extra=router_meta, name='rule_get',
                      summary='解析规则列表')
def rule_get(id: str = None,
              session=Depends(db.get_session),
              user=Security(permission.security, scopes=['group:read'])):
    return ParserGroupView.get_item(session, filter_=[ParserGroup.id == id]).rules


@router_transform.post('/group/{id}/rule', response_model=schema.ParserRuleOut, openapi_extra=router_meta, name='rule_post', summary='解析规则创建')
def rule_post(id: str,
              body: schema.CreateParserRule,
              session=Depends(db.get_session),
              user=Security(permission.security, scopes=['rule:write'])):
    return ParserRuleView.create_item(session, body={**body.dict(), 'group_id': id})


# 解析规则组验证
@router_transform.post('/group/{id}/verify', openapi_extra=router_meta, name='group_verify', summary='数据转换组验证')
def group_verify(id: str,
                 body: schema.GroupVerify,
                 session=Depends(db.get_session),
                 user=Security(permission.security, scopes=['group:write'])):
    return ParserGroupView.verify(session, id, **body.dict())


# 样本数据划词提取
@router_transform.post('/rule/extract-pattern', openapi_extra=router_meta, name='rule_verify',
                   summary='自动提取表达式')
def rule_verify(body: schema.ExtractAutoRegex,
                user=Security(permission.security, scopes=['rule:write'])):
    return ParserRuleView.extract_pattern(**body.dict())


@router_transform.post('/rule/extract', openapi_extra=router_meta, name='rule_verify',
                   summary='自动提取表达式')
def rule_verify(body: schema.ExtractAutoRegex,
                user=Security(permission.security, scopes=['rule:write'])):
    return ParserRuleView.extract_verify(**body.dict())


# 样本数据转换
@router_transform.post('/rule/transform', openapi_extra=router_meta, name='rule_verify',
                   summary='解析规则验证')
def rule_verify(body: schema.Transform,
                user=Security(permission.security, scopes=['rule:write'])):
    return ParserRuleView.transform_verify(**body.dict())


@router_transform.patch('/group/{id}/rule/{rule_id}', response_model=schema.ParserRuleOut, openapi_extra=router_meta, name='rule_patch',
                   summary='解析规则修改')
def rule_patch(id: str,
               rule_id: str,
               body: schema.EditParserRule,
               session=Depends(db.get_session),
               user=Security(permission.security, scopes=['rule:write'])):
    return ParserRuleView.update_item(session, filter_=[ParserRule.id == id], update=body.dict(exclude_unset=True))


@router_transform.delete('/group/{id}/rule', openapi_extra=router_meta, name='rule_delete', summary='解析规则删除')
def rule_delete(id: str,
                body: schema.BatchId,
                session=Depends(db.get_session),
                user=Security(permission.security, scopes=['rule:write'])):
    return {'count': ParserRuleView.delete_items(session, filter_=[ParserRule.group_id == id, ParserRule.id.in_(body.ids)])}


@router_transform.get('/map', response_model=schema.PageParserMaps, openapi_extra=router_meta, name='map_list_get',
                 summary='解析字典列表')
def map_list_get(q: str = None,
             page: int = 1,
             page_size: int = 10,
             order_by: str = 'created_at desc',
             session=Depends(db.get_session),
             user=Security(permission.security, scopes=['map:read'])):
    return ParserMapView.get_page_items(session, q=q, page=page, page_size=page_size, order_by=order_by)


@router_transform.post('/map', response_model=schema.ParserMapOut, openapi_extra=router_meta, name='map_post', summary='解析字典创建')
def map_post(body: schema.CreateParserMap,
              session=Depends(db.get_session),
              user=Security(permission.security, scopes=['map:write'])):
    return ParserMapView.create_item(session, body=body.dict())


@router_transform.get('/map/{id}', response_model=schema.ParserMapOut, openapi_extra=router_meta, name='map_get',
                 summary='解析字典详情')
def map_get(id: str,
             session=Depends(db.get_session),
             user=Security(permission.security, scopes=['map:read'])):
    return ParserMapView.get_item(session, filter_=[ParserMap.id == id])


@router_transform.patch('/map/{id}', response_model=schema.ParserMapOut, openapi_extra=router_meta, name='map_patch',
                   summary='解析字典修改')
def map_patch(id: str,
               body: schema.EditParserMap,
               session=Depends(db.get_session),
               user=Security(permission.security, scopes=['map:write'])):
    return ParserMapView.update_item(session, filter_=[ParserMap.id == id], update=body.dict(exclude_unset=True))


@router_transform.delete('/map', openapi_extra=router_meta, name='map_delete', summary='解析字典删除')
def map_delete(body: schema.BatchId,
                session=Depends(db.get_session),
                user=Security(permission.security, scopes=['map:write'])):
    return {'count': ParserMapView.delete_items(session, filter_=[ParserMap.id.in_(body.ids)])}


@router_transform.get('/lib', response_model=schema.PageParserLibs, openapi_extra=router_meta, name='lib_list_get',
                 summary='解析库列表')
def lib_list_get(q: str = None,
             page: int = 1,
             page_size: int = 10,
             order_by: str = 'created_at desc',
             session=Depends(db.get_session),
             user=Security(permission.security, scopes=['lib:read'])):
    return ParserLibView.get_page_items(session, q=q, page=page, page_size=page_size, order_by=order_by)


@router_transform.post('/lib', response_model=schema.ParserLibOut, openapi_extra=router_meta, name='lib_post', summary='解析库创建')
def lib_post(body: schema.CreateParserLib,
              session=Depends(db.get_session),
              user=Security(permission.security, scopes=['lib:write'])):
    return ParserLibView.create_item(session, body=body.dict())


@router_transform.get('/lib/{id}', response_model=schema.ParserLibOut, openapi_extra=router_meta, name='lib_get',
                 summary='解析库详情')
def lib_get(id: str,
             session=Depends(db.get_session),
             user=Security(permission.security, scopes=['lib:read'])):
    return ParserLibView.get_item(session, filter_=[ParserLib.id == id])


@router_transform.patch('/lib/{id}', response_model=schema.ParserLibOut, openapi_extra=router_meta, name='lib_patch',
                   summary='解析库修改')
def lib_patch(id: str,
               body: schema.EditParserLib,
               session=Depends(db.get_session),
               user=Security(permission.security, scopes=['lib:write'])):
    return ParserLibView.update_item(session, filter_=[ParserLib.id == id], update=body.dict(exclude_unset=True))


@router_transform.delete('/lib', openapi_extra=router_meta, name='lib_delete', summary='解析库删除')
def lib_delete(body: schema.BatchId,
                session=Depends(db.get_session),
                user=Security(permission.security, scopes=['lib:write'])):
    return {'count': ParserLibView.delete_items(session, filter_=[ParserLib.id.in_(body.ids)])}
